package com.example.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 监控工具类
 */
@Slf4j
public class PerformanceUtil {
    /**
     * 监控一段代码的执行时间
     *
     * @param name   名称
     * @param runner
     */
    public static void monitor(String name, ProgramRunner runner) {
        if (StrUtil.isEmpty(name)) {
            name = "";
        }
        long start = System.currentTimeMillis();
        try {
            runner.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        log.info(name + "运行耗时: " + (end - start) + "ms");
    }

    public static void monitor(ProgramRunner runner) {
        monitor(null, runner);
    }
}
